package net.awpspace.mp3player.utils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.awpspace.mp3player.CONSTANT.Constants;
import net.awpspace.mp3player.entity.Mp3File;

public class DAO {
	private static DAO self;
	private static Vector listFile;
	
	public static DAO getInstance(){
		if(self==null)
			self = new DAO();
		return self;
	}
	public DAO(){
		listFile = new Vector();
		readListFile();
	}
	
	private static void readListFile(){
		String url = Constants.FOLDER_PATH;
		try {
			FileConnection localdir = (FileConnection) Connector.open(url,
					Connector.READ_WRITE);
			if (localdir.exists()) {
				System.out.println("ton tai thu muc");
				Enumeration list = localdir.list();
				while (list.hasMoreElements()) {
					String fileName = (String) list.nextElement();
					String filePath = url + "/" + fileName;
					Mp3File mp3 = new Mp3File(filePath, fileName);
					listFile.addElement(mp3);
				}
			} else {
				System.out.println("ko ton tai thu muc");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void updateListFile(){
		listFile.removeAllElements();
		readListFile();
	}
	
	public static Vector getListFile(){
		return listFile;
	}
}
